package cn.livingCloud.controller.content.validator;

import cn.livingCloud.dto.ArticleReplyDto;
import cn.livingCloud.dto.ArticlesReplyRuleFormDto;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import java.util.List;

/**
 * Created by 唐旭 on 2015/12/26.
 */
@Component
public class ArticlesReplyRuleFormDtoValidator extends AbstractRuleDtoValidator implements Validator {
    @Override
    public boolean supports(Class<?> aClass) {
        return ArticlesReplyRuleFormDto.class.equals(aClass);
    }

    @Override
    public void validate(Object target, Errors errors) {
        ArticlesReplyRuleFormDto formDto = (ArticlesReplyRuleFormDto) target;
        validateRule(formDto, errors);
        validateArticleReplyList(formDto, errors);
    }

    private void validateArticleReplyList(ArticlesReplyRuleFormDto formDto, Errors errors) {
        List<ArticleReplyDto> articleList = formDto.getArticles().getArticleList();

        if (isAllEmpty(articleList)) {
            errors.rejectValue("articles.articleList", null, "请至少填写一项图文");
            return;
        }
        //验证每一个的字段是否正确
        for (int index = 0; index < articleList.size(); index++) {
            ArticleReplyDto articleReplyDto = articleList.get(index);
            validateItem(articleReplyDto, errors, index);
        }

    }

    private boolean isAllEmpty(List<ArticleReplyDto> articleReplyList) {
        boolean isAllEmpty = true;
        for (ArticleReplyDto articleReply : articleReplyList) {
            if (isNotEmpty(articleReply)) {
                isAllEmpty = false;
                break;
            }
        }
        return isAllEmpty;
    }

    /**
     * 不为空 表示其中可能填写了某一个
     */
    private boolean isNotEmpty(ArticleReplyDto articleReply) {
        return !(StringUtils.isEmpty(articleReply.getTitle())
                && StringUtils.isEmpty(articleReply.getDescription())
                && StringUtils.isEmpty(articleReply.getPicUrl())
                && StringUtils.isEmpty(articleReply.getUrl()));
    }

    private void validateItem(ArticleReplyDto articleReply, Errors errors, int index) {
        //至少填写一个图文回复
        if (isNotEmpty(articleReply) && StringUtils.isEmpty(articleReply.getTitle())) {
            errors.rejectValue("articles.articleList[" + index + "].title", null, "标题不能为空");
        }
//        if (isNotEmpty(articleReply) && StringUtils.isEmpty(articleReply.getDescription())) {
//            errors.rejectValue("articles.articleList[" + index + "].description", null, "描述不能为空");
//        }
        if (isNotEmpty(articleReply) && StringUtils.isEmpty(articleReply.getPicUrl())) {
            errors.rejectValue("articles.articleList[" + index + "].picUrl", null, "图片路径不能为空");
        }
        if (isNotEmpty(articleReply) && StringUtils.isEmpty(articleReply.getUrl())) {
            errors.rejectValue("articles.articleList[" + index + "].url", null, "图片链接不能为空");
        }
    }
}
